using System;
using System.Collections.Generic;
using System.Text;
using PointBridge.SPAssist;
using Microsoft.SharePoint;

namespace PointBridge.SPDX
{
    public class FeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPDXFeatureReceverHelper helper = new SPDXFeatureReceverHelper();
            helper.Activate(properties.Feature.Parent);
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPDXFeatureReceverHelper helper = new SPDXFeatureReceverHelper();
            helper.Deactivate(properties.Feature.Parent);
        }
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }    
    }

    public class SPDXFeatureReceverHelper : FeatureReceiverHelper
    {
        const string groupName = "PointBridge";
        
        public SPDXFeatureReceverHelper() { }
        public override void Activate(object parent)
        {
            //cast to web (we know it is web)
            SPWeb web = (SPWeb)parent;
            SPAssistant spa = new SPAssistant(web, groupName);

            SPList SPDXConfig = CreateSPDXConfigList(spa);
        }
        public override void Deactivate(object parent)
        {
            //cast to web (we know it is web)
            SPWeb web = (SPWeb)parent;
            SPAssistant sp = new SPAssistant(web, groupName);

            sp.DeleteList("SPDXConfig_" + web.ID);
        }
        
        private SPList CreateSPDXConfigList(SPAssistant spa)
        {
            Guid listId = spa.web.Lists.Add("SPDXConfig_" + spa.web.ID, "This list comntains persistnt configuration for PointBridge SPDX", SPListTemplateType.GenericList);
            SPList list = spa.web.Lists[listId];
            list.ContentTypesEnabled = false;
            list.EnableAttachments = false;
            list.OnQuickLaunch = false;
            list.EnableVersioning = false;
            list.Hidden = true;

            //Provision configuration list
            string name;
            SPField field;

            name = list.Fields.Add("ContentTypesXML", SPFieldType.Note, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);

            name = list.Fields.Add("ListsXML", SPFieldType.Note, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);

            name = list.Fields.Add("Namespace", SPFieldType.Text, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);

            name = list.Fields.Add("IsDeriveFromMyClass", SPFieldType.Boolean, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);

            name = list.Fields.Add("DeriveFromClassName", SPFieldType.Text, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);

            name = list.Fields.Add("OutputDir", SPFieldType.Text, true);
            field = list.Fields.GetField(name);
            spa.SetFieldGroupAndUpdateField(field);
            
            list.Update();

            spa.web.Update();
            return list;
        }
    }
}
